package com.njts.servlet;

import com.njts.auto.AutoCreateBean;
import com.njts.auto.AutoSearch;
import com.njts.bean.Class;
import com.njts.bean.Course;
import com.njts.bean.Subject;
import com.njts.bean.Teacher;
import com.njts.dao.UseDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/courseSelect")
public class CourseSelectServlet extends HttpServlet {
    UseDao useDao=new UseDao();
    AutoCreateBean autoCreateBean=new AutoCreateBean();
    AutoSearch autoSearch=new AutoSearch();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("utf-8");
        Course course=new Course();
        String whereComplementSql="";
        try {
            course=(Course) autoCreateBean.createBean(req, course);
            whereComplementSql = autoSearch.getWhereComplementSql(course);
        } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
        //
        List<Course> courseList = useDao.selectCourse(whereComplementSql);
        ArrayList<Class> classList=(ArrayList<Class>) useDao.selectClass();
        ArrayList<Subject> subjectList=(ArrayList<Subject>) useDao.selectSubject();
        ArrayList<Teacher> teacherList=(ArrayList<Teacher>) useDao.selectTeacher();
        req.setAttribute("classList",classList);
        req.setAttribute("subjectList",subjectList);
        req.setAttribute("teacherList",teacherList);
        req.setAttribute("courseList",courseList);
        req.setAttribute("courseCount",20); //先写死后面改
        req.getRequestDispatcher("/view/course.jsp").forward(req,resp);
    }
}
